package com.ruoyi.controller.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.enums.UserIsAdminType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysChannel;
import com.ruoyi.system.domain.SysChannelOut;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysChannelOutService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.vo.OrderStatisticsRes;
import com.ruoyi.system.vo.WithdrawStatisticsRes;
import com.ruoyi.system.vo.req.SysOrderReq;
import com.ruoyi.system.vo.req.SysWithdrawReq;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysWithdraw;
import com.ruoyi.system.service.ISysWithdrawService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代付订单Controller
 *
 * @author Ericcc
 * @date 2021-03-16
 */
@Controller
@RequestMapping("/system/withdraw")
public class SysWithdrawController extends BaseController
{
    private String prefix = "system/withdraw";

    @Autowired
    private ISysWithdrawService sysWithdrawService;

    @Autowired
    private SysPasswordService sysPasswordService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysChannelOutService sysChannelOutService;

    @RequiresPermissions("system:withdraw:view")
    @GetMapping()
    public String withdraw(ModelMap mmap)
    {
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user",user);
        List<SysChannelOut> sysChannels = sysChannelOutService.selectSysChannelOutList(new SysChannelOut());
        mmap.put("sysChannelOuts",sysChannels);
        return prefix + "/withdraw";
    }

    /**
     * 查询代付订单列表
     */
    @RequiresPermissions("system:withdraw:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysWithdraw sysWithdraw)
    {
        startPage();
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType() == UserIsAdminType.COMMON.getCode()){
            sysWithdraw.setUserCode(user.getUserCode());
        }else if(user.getUserType() == UserIsAdminType.AGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysWithdraw.setParams(map);
            List<SysWithdraw> list = sysWithdrawService.selectSysAgentWithdrawList(sysWithdraw);
            return getDataTable(list);
        }else if(user.getUserType() == UserIsAdminType.HIGHAGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysWithdraw.setParams(map);
            List<SysWithdraw> list = sysWithdrawService.selectSysHighAgentWithdrawList(sysWithdraw);
            return getDataTable(list);
        }
        List<SysWithdraw> list = sysWithdrawService.selectSysWithdrawList(sysWithdraw);
        return getDataTable(list);
    }

    @RequiresPermissions("system:order:list")
    @PostMapping("/showStatistics")
    @ResponseBody
    public WithdrawStatisticsRes showStatistics(SysWithdraw sysWithdraw)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType() == UserIsAdminType.COMMON.getCode()){
            sysWithdraw.setUserCode(user.getUserCode());
        }else if(user.getUserType() == UserIsAdminType.AGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysWithdraw.setParams(map);
            WithdrawStatisticsRes withdrawStatisticsRes = sysWithdrawService.selectSysAgentWithdrawStatistics(sysWithdraw);
            Date todayBegin = DateUtil.beginOfDay(new Date());
            map.put("beginTime",todayBegin.toString());
            sysWithdraw.setParams(map);
            WithdrawStatisticsRes today = sysWithdrawService.selectSysAgentWithdrawStatistics(sysWithdraw);
            withdrawStatisticsRes.setTodayWithdrawAmount(today.getAllWithdrawAmount());
            withdrawStatisticsRes.setTodayWithdrawCharge(today.getAllWithdrawCharge());
            withdrawStatisticsRes.setTodayWithdrawSize(today.getAllWithdrawSize());
            return withdrawStatisticsRes;
        }else if(user.getUserType() == UserIsAdminType.HIGHAGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysWithdraw.setParams(map);
            WithdrawStatisticsRes withdrawStatisticsRes = sysWithdrawService.selectSysHighAgentWithdrawStatistics(sysWithdraw);
            Date todayBegin = DateUtil.beginOfDay(new Date());
            map.put("beginTime",todayBegin.toString());
            sysWithdraw.setParams(map);
            WithdrawStatisticsRes today = sysWithdrawService.selectSysHighAgentWithdrawStatistics(sysWithdraw);
            withdrawStatisticsRes.setTodayWithdrawAmount(today.getAllWithdrawAmount());
            withdrawStatisticsRes.setTodayWithdrawCharge(today.getAllWithdrawCharge());
            withdrawStatisticsRes.setTodayWithdrawSize(today.getAllWithdrawSize());
            return withdrawStatisticsRes;
        }
        WithdrawStatisticsRes withdrawStatisticsRes = sysWithdrawService.selectSysWithdrawStatistics(sysWithdraw);
        Date todayBegin = DateUtil.beginOfDay(new Date());
        Map map = new HashMap();
        map.put("beginTime",todayBegin.toString());
        sysWithdraw.setParams(map);
        WithdrawStatisticsRes today = sysWithdrawService.selectSysWithdrawStatistics(sysWithdraw);
        withdrawStatisticsRes.setTodayWithdrawAmount(today.getAllWithdrawAmount());
        withdrawStatisticsRes.setTodayWithdrawCharge(today.getAllWithdrawCharge());
        withdrawStatisticsRes.setTodayWithdrawSize(today.getAllWithdrawSize());
        return withdrawStatisticsRes;
    }

    /**
     * 导出代付订单列表
     */
    @RequiresPermissions("system:withdraw:export")
    @Log(title = "代付订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysWithdraw sysWithdraw)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        if (user.getUserType() == UserIsAdminType.COMMON.getCode()){
            sysWithdraw.setUserCode(user.getUserCode());
        }else if(user.getUserType() == UserIsAdminType.AGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysWithdraw.setParams(map);
            List<SysWithdraw> list = sysWithdrawService.selectSysAgentWithdrawList(sysWithdraw);
            ExcelUtil<SysWithdraw> util = new ExcelUtil<SysWithdraw>(SysWithdraw.class);
            return util.exportExcel(list, "withdraw");
        }else if(user.getUserType() == UserIsAdminType.HIGHAGENT.getCode()){
            Map map = new HashMap();
            map.put("parentId",user.getUserId());
            sysWithdraw.setParams(map);
            List<SysWithdraw> list = sysWithdrawService.selectSysHighAgentWithdrawList(sysWithdraw);
            ExcelUtil<SysWithdraw> util = new ExcelUtil<SysWithdraw>(SysWithdraw.class);
            return util.exportExcel(list, "withdraw");
        }
        List<SysWithdraw> list = sysWithdrawService.selectSysWithdrawList(sysWithdraw);
        ExcelUtil<SysWithdraw> util = new ExcelUtil<SysWithdraw>(SysWithdraw.class);
        return util.exportExcel(list, "withdraw");
    }

    /**
     * 新增代付订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存代付订单
     */
    @RequiresPermissions("system:withdraw:add")
    @Log(title = "代付订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysWithdrawReq sysWithdraw)
    {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        boolean key = sysPasswordService.matchesPay(sysUser,sysWithdraw.getPayPassword());
        if (!key){
            throw new BusinessException("支付密码错误");
        }
        //存入用户信息
        sysWithdraw.setUserId(sysUser.getUserId());
        sysWithdraw.setUserCode(sysUser.getUserCode());
        return toAjax(sysWithdrawService.insertSysWithdraw(sysWithdraw));
    }

    /**
     * 修改代付订单
     */
    @GetMapping("/edit/{withdrawId}")
    public String edit(@PathVariable("withdrawId") Long withdrawId, ModelMap mmap)
    {
        SysWithdraw sysWithdraw = sysWithdrawService.selectSysWithdrawById(withdrawId);
        mmap.put("sysWithdraw", sysWithdraw);
        return prefix + "/edit";
    }

    /**
     * 修改保存代付订单
     */
    @RequiresPermissions("system:withdraw:edit")
    @Log(title = "代付订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysWithdraw sysWithdraw)
    {
        return toAjax(sysWithdrawService.updateSysWithdraw(sysWithdraw));
    }

}
